package com.example.audiorecord.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.audiorecord.R;
import com.example.audiorecord.adapter.FileListAdapter;
import com.example.audiorecord.audiotools.FileUtil;
import com.example.audiorecord.premissiontools.PermissionsUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AudioListActivity extends AppCompatActivity implements FileListAdapter.onClickFileListListener,SwipeRefreshLayout.OnRefreshListener{
    private RecyclerView audio_list;
    private FileListAdapter adapter;
    private String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO};
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView emptyLayoutHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_list);
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initView() {
        audio_list = findViewById(R.id.audio_list);
        swipeRefreshLayout = findViewById(R.id.refresh_layout);
        emptyLayoutHint = findViewById(R.id.empty_layout_hint);
        swipeRefreshLayout.setOnRefreshListener(this::onRefresh);

        isEmptyLayout();

        adapter = new FileListAdapter();
        adapter.setNewInstance(FileUtil.getWavFiles());
        adapter.setOnClickFileListListener(this::onClick);
        audio_list.setLayoutManager(new LinearLayoutManager(this));
        audio_list.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        audio_list.setAdapter(adapter);
    }

    //判断是否是空布局
    private void isEmptyLayout() {
        if(FileUtil.getWavFiles().size() == 0){
            emptyLayoutHint.setVisibility(View.VISIBLE);
            return;
        }else{
            emptyLayoutHint.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 点击按钮跳转到录音界面
     * 首先检查必要的权限，若没有权限则申请，已经获取权限则执行intent跳转
     * @param view
     */
    public void toMainActivity(View view) {
        PermissionsUtils.getInstance().chekPermissions(AudioListActivity.this, permissions, iPermissionsResult);
    }



    PermissionsUtils.IPermissionsResult iPermissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
            //权限通过
            Intent intent = new Intent(AudioListActivity.this,MainActivity.class);
            startActivity(intent);
        }

        @Override
        public void forbitPermissons() {

        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    public void onClick(File file) {
        Intent intent = new Intent(AudioListActivity.this,PlayerActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("FILE",file);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        isEmptyLayout();
        adapter.setNewInstance(FileUtil.getWavFiles());
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

}